﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using UA.Data;
using UA.Framework;
using UA.Web.Configs;

namespace UA.Web
{
    public class UAHttpApplication : HttpApplication
    {
        /// <summary>
        /// 当 ASP.NET 应用程序成功启动后触发。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void Application_Start(object sender, EventArgs e)
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            SystemEnvironment.InitializeDatabase();

            /// 清空 数据库 中的 Session 记录。
            new SessionAccessor().Delete(s => 1 == 1);
        }

        /// <summary>
        /// 在服务器接收到客户端发起的请求时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 恰好在服务器开始执行事件处理程序时触发。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
        }





        /// <summary>
        /// 在 Request 与 Session 成功关联时触发。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void Application_PostAcquireRequestState(object sender, EventArgs e)
        {
            string id = this.Session.SessionID;
            SessionAccessor accessor = new SessionAccessor();
            var session = accessor.QuerySingle(s => s.SessionID == id);
            if (session != null)
            {
                session.LastRequestDate = DateTime.Now;
                accessor.Edit(session, "LastRequestDate");
            }
        }






        /// <summary>
        /// 当服务器为客户端分配新的会话状态 Session 时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void Session_OnStart(object sender, EventArgs e)
        {
            Application.Lock();
            string id = this.Session.SessionID;
            if (!string.IsNullOrWhiteSpace(id))
            {
                SessionAccessor accessor = new SessionAccessor();
                var s = accessor.QuerySingle(item => item.SessionID == id);
                if (s != null)
                {
                    s.LastRequestDate = DateTime.Now;
                }
                else
                {
                    accessor.Add(new { SessionID = this.Session.SessionID, LastRequestDate = DateTime.Now });
                }
            }
            Application.UnLock();
        }

        /// <summary>
        /// 当服务器中的客户端会话状态 Session 执行 Abandon 方法或状态过期时触发（仅限 SessionState.Mode = "InProc" 时）。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void Session_OnEnd(object sender, EventArgs e)
        {
            Application.Lock();
            string id = this.Session.SessionID;
            if (!string.IsNullOrWhiteSpace(id))
            {
                new SessionAccessor().Delete(s => s.SessionID == id);
            }
            Application.UnLock();
        }
    }
}
